﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System.Web.UI;
using EPiServer.Core;

namespace EPiServer.Municipality.Web.UI.WebControls {
    /// <summary>
    /// Summary description for CustomValidationSummary.
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class CustomValidationSummary : System.Web.UI.WebControls.ValidationSummary {
        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
        #region private string[] GetErrorMessages(out bool inError)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inError"></param>
        /// <returns></returns>
        private string[] GetErrorMessages(out bool inError) {
            string[] strArray = null;
            inError = false;
            int num = 0;
            ValidatorCollection validators = Page.GetValidators(ValidationGroup);
            for (int i = 0; i < validators.Count; i++) {
                IValidator validator = validators[i];
                if (!validator.IsValid) {
                    inError = true;
                    if (validator.ErrorMessage.Length != 0) {
                        num++;
                    }
                }
            }
            if (num != 0) {
                strArray = new string[num];
                int index = 0;
                for (int j = 0; j < validators.Count; j++) {
                    IValidator validator2 = validators[j];
                    if ((!validator2.IsValid && (validator2.ErrorMessage != null)) && (validator2.ErrorMessage.Length != 0)) {
                        strArray[index] = string.Copy(validator2.ErrorMessage);
                        index++;
                    }
                }
            }
            return strArray;
        }
        #endregion
        #region protected override void Render(HtmlTextWriter writer)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer) {
            if (Page.IsPostBack) {
                try {
                    bool hasErrors;
                    string[] errorMessages = GetErrorMessages(out hasErrors);

                    if (hasErrors) {
                        writer.AddAttribute("class", "systemwarning");
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);
                        writer.RenderBeginTag(HtmlTextWriterTag.Strong);
                        writer.Write(LanguageManager.Instance.Translate("/municipality/global/systemwarning"));
                        writer.RenderEndTag();
                        writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                        for (int i = 0; i < errorMessages.Length; i++) {
                            writer.RenderBeginTag(HtmlTextWriterTag.Li);
                            writer.Write(errorMessages[i]);
                            writer.RenderEndTag();
                        }
                        writer.RenderEndTag();
                        writer.RenderEndTag();
                        writer.RenderEndTag();
                        writer.RenderEndTag();

                    }
                    // ReSharper disable EmptyGeneralCatchClause
                } catch { }
                // ReSharper restore EmptyGeneralCatchClause
            }
        }
        #endregion
    }
}